package com.Date;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MainDate {
    public static void main(String[] args) {
        Date date = new Date();
        int seconds = 17854;// 秒
        int millisecond = 360000; // 毫秒
        long time = date.getTime();
        Model model = new Model();
        System.out.println(model.userName);

        System.out.println("时间戳： " + time);
        System.out.println(secToTime(seconds));
        System.out.println(millisecond + "毫秒转换格式时间：\t" + millisecondToTime(millisecond));
        System.out.println("当前时间（时:分：秒.毫秒）\t" + new SimpleDateFormat("HH:mm:ss.SSSS").format(new Date()));
        date.setTime(1627546581L);
        System.out.println("当前时间（时:分：秒.毫秒）\t" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS").format(date));
        getYearMonthDay();

        formatDate();

        System.out.println("startDate" + new Date(0L));
        System.out.println("endDate:" + new Date(System.currentTimeMillis()));
        System.out.println("endDate:" + new Date());
    }

    private static void formatDate() {

        //y 代表年
        //M 代表月
        //d 代表日
        //H 代表24进制的小时
        //h 代表12进制的小时
        //m 代表分钟
        //s 代表秒

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = new Date();
        String str = sdf.format(d);
        System.out.println("当前时间通过 yyyy-MM-dd HH:mm:ss SSS 格式化后的输出: \n" + str);

        sdf = new SimpleDateFormat("yyyy-MM-dd");
        str = sdf.format(d);
        System.out.println("当前时间通过 yyyy-MM-dd 格式化后的输出: \n" + str);


        sdf = new SimpleDateFormat("yyyy-MM-dd 'T' HH:mm:ss");
        str = sdf.format(d);
        System.out.println("当前时间通过 yyyy-MM-ddTHH:mm:ss 格式化后的输出：\n" + str);

    }

    /**
     * 获取当前时间的年月日
     */
    private static void getYearMonthDay() {
        Calendar calendar = Calendar.getInstance();
        System.out.println("年 ：" + calendar.get(Calendar.YEAR));
        System.out.println("月 ：" + calendar.get(Calendar.MONTH) + 1);
        System.out.println("日 ：" + calendar.get(Calendar.DATE));
    }

    /**
     * 秒转换小时-分-秒analytics/util/DateUtil.java
     *
     * @param seconds 秒为单位 比如..600秒
     * @return 比如...2小时3分钟52秒
     */
    public static String secToTime(int seconds) {
        int hour = seconds / 3600;
        int minute = (seconds - hour * 3600) / 60;
        int second = (seconds - hour * 3600 - minute * 60);
        StringBuilder sb = new StringBuilder();
        if (hour > 0) {
            sb.append(hour).append("小时");
        }
        if (minute > 0) {
            sb.append(minute).append("分");
        }
        if (second > 0) sb.append(second).append("秒");
        else if (second == 0) sb.append("<1秒");
        return sb.toString();
    }

    /**
     * 将int类型数字转换成时分秒毫秒的格式数据
     *
     * @param time long类型的数据
     * @return HH:mm:ss.SSS
     * @author zero 2019/04/11
     */
    public static String millisecondToTime(int time) {
        String timeStr;
        int hour;
        int minute;
        int second;
        int millisecond;
        if (time <= 0) return "00:00:00.000";
        else {
            second = time / 1000;
            minute = second / 60;
            millisecond = time % 1000;
            if (second < 60) {
                timeStr = "00:00:" + unitFormat(second) + "." + unitFormat2(millisecond);
            } else if (minute < 60) {
                second = second % 60;
                timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second) + "." + unitFormat2(millisecond);
            } else {// 数字>=3600 000的时候
                hour = minute / 60;
                minute = minute % 60;
                second = second - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second) + "." + unitFormat2(millisecond);
            }
        }
        return timeStr;
    }

    public static String unitFormat(int i) {// 时分秒的格式转换
        String retStr;
        if (i >= 0 && i < 10) retStr = "0" + i;
        else retStr = "" + i;
        return retStr;
    }

    public static String unitFormat2(int i) {// 毫秒的格式转换
        String retStr;
        if (i >= 0 && i < 10) retStr = "00" + i;
        else if (i >= 10 && i < 100) {
            retStr = "0" + i;
        } else retStr = "" + i;
        return retStr;
    }
}
